<template>
  <div class="pageheader">
    <el-page-header @back="back">
      <template #content>
        <span> 方案比选 </span>
      </template>
    </el-page-header>
  </div>
  <div style="margin: 20px">
    <el-card>
      <el-table ref="viewTable" :data="tableData" :border="true" highlight-current-row class="dataTable">
        <el-table-column sortable prop="solutionId" label="ID" width="65" fixed="left"></el-table-column>
        <el-table-column sortable prop="name" label="方案名称" width="105" fixed="left">
          <template v-slot="scope">
            {{ getNameById(scope.row.solutionId) }}
          </template>
        </el-table-column>
        <el-table-column sortable prop="3.3" label="（改革前）本期账面价值">
          <template v-slot="scope">
            {{ scope.row['3.3'].toFixed(2) + "万元" }}
          </template>
        </el-table-column>
        <el-table-column sortable prop="4.3" label="（改革后）本期账面价值">
          <template v-slot="scope">
            {{ scope.row['4.3'].toFixed(2) + "万元" }}
          </template>
        </el-table-column>
        <el-table-column sortable prop="5.1" label="本期账面价值差值">
          <template v-slot="scope">
            {{ scope.row['5.1'].toFixed(2) + "万元" }}
          </template>
        </el-table-column>
        <el-table-column sortable prop="3.5" label="（改革前）本期现值">
          <template v-slot="scope">
            {{ scope.row['3.5'].toFixed(2) + "万元" }}
          </template>
        </el-table-column>
        <el-table-column sortable prop="4.5" label="（改革后）本期现值">
          <template v-slot="scope">
            {{ scope.row['4.5'].toFixed(2) + "万元" }}
          </template>
        </el-table-column>
        <el-table-column sortable prop="5.2" label="本期现值差值">
          <template v-slot="scope">
            {{ scope.row['5.2'].toFixed(2) + "万元" }}
          </template>
        </el-table-column>
        <el-table-column sortable prop="3.6" label="（改革前）累计现值">
          <template v-slot="scope">
            {{ scope.row['3.6'].toFixed(2) + "万元" }}
          </template>
        </el-table-column>


        <el-table-column sortable prop="4.6" label="（改革后）累计现值">
          <template v-slot="scope">
            {{ scope.row['4.6'].toFixed(2) + "万元" }}
          </template>
        </el-table-column>


        <!-- <el-table-column prop="selection" label="选择方案" fixed="right" width="120">
            <template v-slot="scope">
                <el-button size="small" @click="selectBest(scope.row)">选择方案</el-button>
            </template>
        </el-table-column> -->
      </el-table>
    </el-card>
  </div>
</template>
<script lang="ts" setup>
import {ElMessage, formContextKey} from 'element-plus';
import solutionApi from "@/api/repair/solution";
import {reactive, ref} from 'vue';
import router from "@/router";

const tableData = ref([])
const solutionMap = ref({} as any)
const notEmpty = ref(false)
// 获取方案比选结果
const getCompare = (ids: Array<number>) => {
  solutionApi.compare(ids).then((res) => {
    console.log(res)
    tableData.value = res.data.data
  })
}

// 回退
const back = () => {
  router.push("/repair/solution");
};

// 根据方案id获取方案名称
const getNameById = (id: string) => {
  return solutionMap[id]
}

// 选择最佳方案生成报告
const selectBest = (solution: any) => {
  console.log(solution)
  window.sessionStorage.setItem("best", JSON.stringify(solution.solutionId))
  router.push("/repair/report")
}

// 进行渲染
const infraCompare = window.sessionStorage.getItem("repairCompare")
if (infraCompare === null) {
  ElMessage.error("尚未选择比选方案！")
  notEmpty.value = false;
} else {
  notEmpty.value = true;
  const solutionList = JSON.parse(infraCompare)
  const ids = []
  for (const item of solutionList) {
    ids.push(item.id)
    solutionMap[item.id] = item.name
  }
  getCompare(ids)
}
</script>